﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

public partial class Banner_Maintain : PageBase
{
    CHAINS chain;
    private int id=0,chainID=0;
    private const string CHANNEL_ID = "id";
    private const string CHAIN_ID = "chainID";
    private const string RETURN_PAGE = "Chain_Maintain.aspx?id=";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[CHANNEL_ID] != null)
        {
            int.TryParse(Request.QueryString[CHANNEL_ID], out id);
        }
        if (Request.QueryString[CHAIN_ID] != null)
        {
            int.TryParse(Request.QueryString[CHAIN_ID], out chainID);
            if (chainID > 0)
            {
                chain = new ChainManager().Load(chainID);
                if (chain != null)
                {
                    lblChainName.Text = chain.DESCRIPTION;
                }
            }
        }
        if (!IsPostBack )
        {
            this.FillForm();
        }
    }


    private void FillForm()
    {
        
        if (id>0)
        {
            BANNERS channel = new BannerManager().Load(id);
            if (channel != null)
            {
                txtName.Text = channel.DESCRIPTION;
                txtCode.Text = channel.CODE;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            BANNERS channel;
            if (id > 0)
            {
                channel = new BannerManager().Load(id);
            }
            else
            {
                channel = new BANNERS();
            }
            channel.CODE = txtCode.Text;
            channel.CHAINCODE = chain != null ? chain.CODE : "";
            channel.DESCRIPTION = txtName.Text;
            channel.DOMAINID = this.MyStateManager.DomainID;
            channel.LAST_UPDATER = this.MyStateManager.Name;
            channel.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));

            new BannerManager().Save(channel);
            Response.Redirect(RETURN_PAGE + chainID.ToString());
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(RETURN_PAGE + chainID.ToString());
    }

}
